import React, { Component } from 'react'
import store from '../../redux/store'


export default class Count extends Component {
    
    
    increment=()=>{
        const {value} = this.selectNumbers
        store.dispatch({type:'increment',data:parseInt(value)})
    }
    
    decrement=()=>{
        const {value} = this.selectNumbers
        store.dispatch({type:'decrement',data:parseInt(value)})
        
    }
    incrementifOdd=()=>{
        const {value} = this.selectNumbers
        const sum = store.getState()
        if(parseInt(sum)%2!==0){
            store.dispatch({type:'increment',data:parseInt(value)})
        }
    }
    incrementAsync=()=>{
        const {value} = this.selectNumbers
        
        setTimeout(()=>{
            store.dispatch({type:'increment',data:parseInt(value)})
        },3000)
    }
    /*
    componentDidMount(){
        store.subscribe(()=>{
            //this.setState({})
            this.forceUpdate()
        })
    }*/
    render() {
        console.log(store.getState())
        const sum = store.getState()
        return (
            <div>
                <h1>当前求和为：{sum}</h1>
                <select ref={c => this.selectNumbers = c }>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                </select>&nbsp;
                <button onClick={this.increment}>+</button>&nbsp;
                <button onClick={this.decrement}>-</button>&nbsp;
                <button onClick={this.incrementifOdd}>当前求和为奇数+</button>&nbsp;
                <button onClick={this.incrementAsync}>异步+</button>
            </div>
        )
    }
}
